#!/usr/bin/perl -w
## correct alignWigToBed or MethylpatternAligfnment matrix by COMPARE tools
## author: Yaping Liu  lyping1986@gmail.com
##cmd to use COMPARE: perl /export/uec-gs1/laird/users/yaping/software/external/COMPARE_1.0/COMPARE.pl input_matrix output_dir 10 1  

use strict;
use Getopt::Long;

my $use_alignWig_style="";
my $omit_cutoff="";
my $omit_COMPARE_format="";
my $omit_correction="";
my $make_average="";
GetOptions( 
	"use_alignWig_style" => \$use_alignWig_style,
	"omit_cutoff" => \$omit_cutoff,
	"omit_COMPARE_format" => \$omit_COMPARE_format,
	"omit_correction" => \$omit_correction,
	"make_average" => \$make_average,
);

my $prefix=$ARGV[0] || die "no prefix";
my $dir=$ARGV[1] || die "no dir";
my $input=$ARGV[2] || die "no input matrix";;

my $COMPAREDIR="/export/uec-gs1/laird/users/yaping/software/external/COMPARE_1.0/";
my $COMPARE="perl $COMPAREDIR/COMPARE.pl";
my $PLOT_ON_COMPARE="/home/rcf-40/yapingli/storage/code/mytools/R/COMPARE_matrix_average_Plot.R";

##1.cut off column 1-4 for the position information
my $noHeaderTmp=$prefix.".noHeader.txt";
#$noHeaderTmp=~s/\.\w+$/.noHeader.txt/;
my $cmd = "cut -f5-2405 $input > $noHeaderTmp\n";
if($omit_cutoff eq ""){
	print STDERR $cmd;
	system($cmd) == 0 || die "not complete cut off 1-4 columns:$!\n";
}


##2. replace NA to be NaN, and change value from 0-100 range to 0-1 range. change "\t" delimit matrix into " " delimited matrix
my $COMPARE_Format=$noHeaderTmp;
$COMPARE_Format=~s/\.txt$/.COMPARE_Format.txt/;
if($use_alignWig_style ne ""){
	
}
$cmd = "sed 's/\\t/ /g' $noHeaderTmp > $COMPARE_Format\n";


if($omit_COMPARE_format eq ""){
	print STDERR $cmd;
	system($cmd) == 0 || die "not complete format change to COMPARE style:$!\n";
	`mv $COMPARE_Format $noHeaderTmp`;
	$cmd = "sed 's/0\\.0 /0 /g' $noHeaderTmp > $COMPARE_Format\n";
	print STDERR $cmd;
	system($cmd) == 0 || die "not complete format change to COMPARE style:$!\n";
	`mv $COMPARE_Format $noHeaderTmp`;
	$cmd = "sed 's/0\\.0\$/0/g' $noHeaderTmp > $COMPARE_Format\n";
	print STDERR $cmd;
	system($cmd) == 0 || die "not complete format change to COMPARE style:$!\n";
	
	$cmd = "unlink $noHeaderTmp\n";
	print STDERR $cmd;
	system($cmd) == 0 || die "not able to delete file $noHeaderTmp:$!\n";
}


##3. ##run COMPARE to do correction

if($omit_correction eq ""){
	if(-e $dir){
		`rm -r $dir`;
		print STDERR "rm -r $dir\n";
	}
	$cmd = "$COMPARE $COMPARE_Format $dir 10 1 $COMPAREDIR\n";
	print STDERR $cmd;
	system($cmd) == 0 || die "not complete COMPARE correction:$!\n";
}

##4. make average plot on the COMPARE corrected matrix
if($make_average ne ""){
	my $matrix_after_correction = "Inference_Matrix.allinf";
	my $output_fig = "Inference_Matrix.allinf.afterCorrection.pdf";
	##die "COMPARE correction not finished...\n\n" unless -e $matrix_after_correction;
	$cmd = "R --no-restore --no-save --args wd=$dir output=$output_fig input=$matrix_after_correction < $PLOT_ON_COMPARE\n";
	print STDERR $cmd;
	system($cmd) == 0 || die "not complete average plot after COMPARE correction:$!\n";
}
